﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;

namespace Feniks
{
    public enum Settings
    {
        ServerName,
        DatabaseName,
        AuthenticateMode,
        UserName,
        Password,
        ScalePort,
        UrunEtiketiYazici,
        RafEtiketiYazici
    }

    public class Sessions
    {
        private Sessions() { _sessions = new Dictionary<string, object>(); }
        private static Sessions _instance;

        public static Sessions CreateSession
        {
            get
            {
                if (_instance == null)
                    _instance = new Sessions();
                _instance.readAllSettings();
                return _instance;
            }
        }

        public void CloseSession()
        {
            saveAllSettings();
        }

        private Dictionary<string, object> _sessions;

        public object this[string key]
        {
            get
            {
                if (_sessions.ContainsKey(key))
                    return _sessions[key];
                else
                    return "";
            }
            set
            {
                if (_sessions.ContainsKey(key))
                    _sessions[key] = value;
                else
                    _sessions.Add(key, value);
            }
        }

        public object this[Settings key]
        {
            get
            {
                if (_sessions.ContainsKey(key.ToString()))
                    return _sessions[key.ToString()];
                else
                    return "";
            }
            set
            {
                if (_sessions.ContainsKey(key.ToString()))
                    _sessions[key.ToString()] = value;
                else
                    _sessions.Add(key.ToString(), value);
            }
        }

        private void readAllSettings()
        {
            string filePath = Application.StartupPath + "\\ayarlar.ini";
            if (File.Exists(filePath))
            {
                using (StreamReader sr = new StreamReader(filePath))
                {
                    string line;
                    string[] fields;
                    while (sr.Peek() > -1)
                    {
                        line = sr.ReadLine();
                        fields = line.Split(';');
                        _sessions.Add(fields[0], fields[1]);
                    }
                    sr.Close();
                }
            }
        }

        private void saveAllSettings()
        {
            string filePath = Application.StartupPath + "\\ayarlar.ini";
            using (StreamWriter sw = new StreamWriter(filePath, false))
            {
                foreach (KeyValuePair<string, object> item in _sessions)
                {
                    sw.WriteLine(string.Format("{0};{1}", item.Key, item.Value));
                }
                sw.Close();
            }
        }

    }
}
